package com.wc.算法提高课.A第一章_动态规划.区间DP.加分二叉树;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/5/12 19:04
 * @description https://www.acwing.com/problem/content/481/
 */
public class Main {
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 30;
    static int[] w = new int[N];
    static int[][] f = new int[N][N];
    static int[][] g = new int[N][N];
    static int n;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1; i <= n; i++) {
            w[i] = sc.nextInt();
            f[i][i] = w[i];
            g[i][i] = i;
        }

        for (int len = 2; len <= n; len++) {
            for (int l = 1; l + len - 1 <= n; l++) {
                int r = l + len - 1;
                for (int k = l; k <= r; k++) {
                    int left = l == k ? 1 : f[l][k - 1];
                    int right = r == k ? 1 : f[k + 1][r];
                    int v = w[k] + left * right;
                    if (f[l][r] < v) {
                        f[l][r] = v;
                        g[l][r] = k;
                    }
                }
            }
        }
        out.println(f[1][n]);
        dfs(1, n);
        out.flush();
    }

    static void dfs(int l, int r) {
        if (l > r) return;
        int root = g[l][r];
        out.print(root + " ");
        dfs(l, root - 1);
        dfs(root + 1, r);
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
